<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tools :tree-node="company" :is-root="true"></tree-tools>
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            :is-root="false"
            @addDepts="addDepts"
            @delDepts="getDepartments"
          ></tree-tools>
        </el-tree>
      </el-card>
      <add-dept
        :show-dialog="showDialog"
        :tree-node="node"
        @addDepts="getDepartments"
        @changedialog="hideDialog"
      />
    </div>
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import AddDept from './components/add-dept'
export default {
  components: {
    treeTools,
    AddDept
  },
  data() {
    return {
      node: {},
      showDialog: false, // 显示窗体
      company: { name: '北京演义学院', manager: '负责人' },
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      departs: []
    }
  },
  computed: {},
  created() {
    this.getDepartments()
  },
  mounted() {},

  methods: {
    async getDepartments() {
      const res = await getDepartments()
      console.log('res:', res)
      this.departs = tranListToTreeData(res.depts, '')
      console.log('departs:', this.departs)
    },
    addDepts(node) {
      this.showDialog = true // 显示弹层
      // 因为node是当前的点击的部门， 此时这个部门应该记录下来,
      this.node = node
    },
    hideDialog(value) {
      this.showDialog = value
    }
  }
}
</script>
<style lang="scss" scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
